define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/withdraw/index' + location.search,
                    add_url: 'user/withdraw/add',
                    edit_url: 'user/withdraw/edit',
                    del_url: 'user/withdraw/del',
                    multi_url: 'user/withdraw/multi',
                    import_url: 'user/withdraw/import',
                    audit_url: 'user/withdraw/audit',//添加的字段
                    audit_arr_url: 'user/withdraw/auditpass',//添加的字段
                    table: 'user_withdraw',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams:function(params){
                    var op =  {};
                    var filter = {};
                    filter.auditStatus = 0;
                    op.auditStatus = '=';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        //主键
                       // {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'memberName', title: __('会员姓名'), operate: 'LIKE'},
                        {field: 'cardNo', title: __('银行卡号'), operate: 'LIKE'},
                        {field: 'idCard', title: __('身份证号'), operate: 'LIKE'},
                        {field: 'payType', title: __('Paytype'), searchList: {"0":__('Paytype 0'),"1":__('Paytype 1'),"2":__('Paytype 2'),"3":__('Paytype 3')}, formatter: Table.api.formatter.normal},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        //手续费
                      //  {field: 'service_money', title: __('Service_money'), operate:'BETWEEN'},
                        {field: 'pay_money', title: __('Pay_money'), operate:'BETWEEN'},
                        //创建时间
                        {field: 'createtime', title: __('申请时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        {field: 'orderNo', title: __('Orderno'), operate: 'LIKE'},
                        {field: 'payNo', title: __('Payno'), operate: 'LIKE'},
                        //支付时间
                        //  {field: 'payTime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //支付失败信息
                        {field: 'payMsg', title: __('Paymsg'), operate: 'LIKE'},
                        {field: 'auditStatus', title: __('Auditstatus'), 
                            searchList: {"0":__('Auditstatus 0'),"1":__('Auditstatus 1'),"2":__('Auditstatus 2')},
                            formatter: Table.api.formatter.status},
                        //审核失败原因
                      //  {field: 'auditMsg', title: __('Auditmsg'), operate: 'LIKE'},
                        //审核用户
                      //  {field: 'auditUsername', title: __('Auditusername'), operate: 'LIKE'},
                        //审核用户id
                       // {field: 'auditUserId', title: __('Audituserid')},
                        {field: 'auditTime', title: __('Audittime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        
                       //更新时间
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //备注
                     //   {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        //角色
                        //{field: 'role', title: __('Role'), operate: 'LIKE'},
                        //网体
                        //{field: 'reticulum', title: __('Reticulum'), operate: 'LIKE'},
                        //张贴
                        //{field: 'post', title: __('Post'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [{name: 'audit', text: '审核', title: '审核', classname: 'btn btn-xs btn-primary btn-dialog', url: 'user/withdraw/audit',disable: function(row){
                                    if (row.payType != 0) {
                                        return true;
                                    }
                                }, //判断按钮是否禁用
                            }]
                        }
                    ]
                ]
            });
            //当表格数据加载完成时 接收json传过来的附加值
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                // 这里我们手动设置底部的值
                $("#noauditStatus").text(data.extend.noauditStatus);
                $("#pay").text(data.extend.pay);
                $("#finishpay").text(data.extend.finishpay);
            });
            // 自定义搜索条件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var that = $(this);

                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    params = queryParams(params);
                    let fielddata = that.data("field")
                    var op = params.op ? JSON.parse(params.op) : {};
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    if (fielddata == 'with_type') {
                            params.with_type = that.data("value");
                    }else if(fielddata){
                        if (fielddata == 'auditStatus' && op.payType) {
                            delete op.payType
                            delete filter.payType
                        }else if (fielddata == 'payType' && op.auditStatus) {
                            delete op.auditStatus
                            delete filter.auditStatus
                        }

                        // var op =  {};
                        // var filter = {};
                        filter[fielddata] = that.data("value");
                        op[fielddata] = '=';
                        params.op = JSON.stringify(op);
                        params.filter = JSON.stringify(filter);
                    }else{
                        delete op.auditStatus
                        delete op.payType
                        delete filter.payType
                        delete filter.auditStatus
                        params.op = JSON.stringify(op);
                        params.filter = JSON.stringify(filter);
                    }

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        audit: function () {
           Controller.api.bindevent();
        },
        much: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/withdraw/much',
                    import_url: 'user/withdraw/much',
                },
                defaults:{
                    pageList:['All']
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                fixedColumns: true,
                fixedRightNumber: 1,
                pageList:['All'],
                columns: [
                    [
                        {field: 'id', title: __('序号')},
                        {field: 'orderNo', title: __('订单号')},
                        {field: 'pay_money', title: __('金额')},
                        {field: 'memberName', title: __('姓名')},
                        {field: 'idCard', title: __('身份证')},
                        {field: 'phone', title: __('手机')},
                        {field: 'cardNo', title: __('银行卡号')},
                        {field: 'msg', title: __('信息')}
                    ]
                ]
            });
            $("#excelimport").data("upload-success", function(data){
                console.log(data);
            });
            require(['upload'], function (Upload) {
                Upload.api.plupload($(".btn-import"), function (data, ret) {
                    Fast.api.ajax({
                        url: "user/withdraw/much", //上传接收地址
                        data: {file: data.url},
                        success: function (response) {
                            console.log(response);
                            $('#table').bootstrapTable('load', response);
                            layer.closeAll();
                        }
                    }, function (data, ret) {
                        // console.log(data);
                        // console.log(ret);
                    });
                });
            });
            //当表格数据加载完成时 接收json传过来的附加值
            // table.on('load-success.bs.table', function (e, data) {
            //     //这里可以获取从服务端获取的JSON数据
            //     //这里我们手动设置底部的值
            //     $("#noauditStatus").text(data.extend.noauditStatus);
            //     $("#pay").text(data.extend.pay);
            //     $("#finishpay").text(data.extend.finishpay);
            // });
            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
